"""

Main config file for the schema.

This file may contain a number of config functions that provide alternative 
configurations for this schema. The user can select which function will be 
selected through the user interface.

Each config function needs to:
- create a config api object using the factory function ConfigAPIFactory()
- set the maximum number of individuals using setMaxIndividuals()
- set any data to be shared between all scripts using setGlobalData()
- set the data structure of individuals using setDataStructure()
- set each script that may be executed using setTask()
"""

from dexen.user.libs.server_api import ConfigAPI

# Constants from the system and for this schema
from dexen.user.libs import constants as dc
from cubes.tasks.libs import constants as sc

def config_synch():
    """
    
    Main config function that can be selected in the user interface.
    
    This config produces a synchronous evolutionary process.
    
    """
    
    #Get the API object (this must be done inside this function).
    config_api = ConfigAPIFactory()
    
    # Set the maximum number of individuals in the population. When this number
    # is reached, evolution will stop.
    config_api.setMaxIndividuals(50000)
    
    # Set global constants that will be saved on the server.
    # These constants set the numbers of individuals to be downloaded by each 
    # script.
    config_api.setGlobalData({constants.INITIALIZATION : 200,
                               constants.DEVELOPMENT : 200,
                               constants.EVALUATION : 200,
                               constants.VIZUALIZATION : 200,
                               constants.FEEDBACK : 200})
    
    
    # Execute shared functions that set:
    # -  the data structure for individuals and
    # -  the tasks to be executed
    _set_data_structure(config_api)
    _set_tasks(config_api)


def config_asynch():
    """
    
    Main config function that can be selected in the user interface.
    
    This config produces a synchronous evolutionary process.
    
    """
    
    #Get the API object (this must be done inside this function).
    config_api = server_api.ConfigAPIFactory()
    
    # Set the maximum number of individuals in the population. When this number
    # is reached, evolution will stop.
    config_api.setMaxIndividuals(50000)
    
    # Set global constants that will be saved on the server.
    # These constants set the numbers of individuals to be downloaded by each 
    # script.
    config_api.setGlobalData({constants.INITIALIZATION : 200,
                               constants.DEVELOPMENT : 10,
                               constants.EVALUATION : 10,
                               constants.VIZUALIZATION : 10,
                               constants.FEEDBACK : 50})
    
    # Execute shared functions that set:
    # -  the data structure for individuals and
    # -  the tasks to be executed
    _set_data_structure(config_api)
    _set_tasks(config_api)
    
def _set_data_structure(config_api):
    """
    
    Shared private function that sets the data structure for individuals
    in the population.
    
    """
    config_api.setDataStructure(
        attributes = {
            sc.GENOTYPE : dc.BLOB,
            sc.PHENOTYPE : dc.BLOB,
            sc.WIDTH : dc.FLOAT,
            sc.HEIGHT : dc.FLOAT,
            sc.MODEL : dc.BLOB,
            sc.IMAGE : dc.BLOB
        }
    )

def _set_graphs(config_api):
    """
    """
    config_api.setGraphs(
        objectives=(
            (sc.WIDTH, dc.MAXIMIZE, "Width (m2)"),
            (sc.HEIGHT, dc.MAXIMIZE, "Width (m2)")
        ),
        step_time=100,
        model=sc.MODEL,
        image=sc.IMAGE
    )

def set_pareto(config_api):
        script_name = "initialization",  
        file_name = "tasks.wrappers.gui_wrapper",
        function_name = "createGraphs",
        is_looped = True

        
    
def _set_tasks(config_api):
    """
    
    Shared private function that sets the tasks within this schema.
    
    """
    config_api.setTask(
        script_name = "initialization",             
        file_name = "tasks.wrappers.initialization_wrapper", 
        function_name = "initialise",
    )
    
    config_api.setTask(
        script_name = "development", 
        file_name = "tasks.wrappers.development_wrapper", 
        function_name = "develop", 
    )
    
    config_api.setTask(
        script_name = "evaluation", 
        file_name = "tasks.wrappers.evaluation_wrapper", 
        function_name = "evaluate",
    )
    
    config_api.setTask(
        script_name = "visualization",
        file_name = "tasks.wrappers.visualization_wrapper",
        function_name = "visualize"
    )
    
    config_api.setTask(
        script_name = "feedback",    
        file_name = "tasks.wrappers.feedback_wrapper", 
        function_name = "feedback",
    )
    
    config_api.setTask(
        name = "graphs",
        func_path = "tasks.wrappers.gui_wrapper.createGraphs",
        select = (),
        where = (),
        count = None
    )

    
